# -*- coding: utf-8 -*-
# @Time    : 2024/3/1 9:56
# @Author  : Alvin
# @File    : parse_data.py
from datetime import datetime
from .minio_service import get_image_preview_url
import json
def parse_note_data(note_data, for_web = False):
    '''

    :param note_data: 传进来的笔记数据
    :param for_web: 是否用于爬虫网站，如果True则运用obs服务更新图片资源url
    :return:
    '''
    # note_data = future.result()
    note_id = note_data['id']
    note_card = note_data['note_card']
    note_content = note_card.get('desc','无')

    note_title = note_card['title']
    img_url_list = []
    # print(note_card)
    if 'image_list' in note_card:
        for img_url in note_card['image_list']:
            image_url = img_url['url_default']
            # print(f"==========={img_url}")
            if for_web:
                image_url = get_image_preview_url(image_url)
            img_url_list.append(image_url)
    else:
        img_url_list = '无'
    if "video" in note_card:
        video = note_card['video']
        duration = video['capa']['duration']
        video_url = video['media']['stream']['h264'][0]['master_url']
        # video_info = {"video_url": video_url, "duration": duration}
    else:
        # video_info = '无'
        duration = 0
        video_url = '无'
    interact_info = note_card['interact_info']

    liked_count = interact_info['liked_count']

    collected_count = interact_info.get('collected_count', 0)
    comment_count = interact_info.get('comment_count', 0)
    share_count = interact_info.get('share_count', 0)

    last_update_time = note_card['last_update_time']
    timestamp = int(last_update_time) / 1000  # 将毫秒转换为秒
    formatted_date = datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
    ip_location = interact_info.get('ip_location','无')

    tag_list = []
    for tag in note_card['tag_list']:
        tag_list.append(tag['name'])
    user = note_card['user']
    if for_web:
        avatar = get_image_preview_url(user['avatar'])
    else:
        avatar = user['avatar']

    nickname = user['nickname']
    user_id = user['user_id']
    return {
        'note_id': note_id,
        'note_title': note_title,
        'note_content': note_content,
        'img_url_list': img_url_list,
        # 'video_info': video_info,
        'video_url':video_url,
        'duration':duration,
        'collected_count': collected_count,
        'comment_count': comment_count,
        'liked_count': liked_count,
        'share_count': share_count,
        'tag_list': tag_list,
        'ip_location': ip_location,
        'user_id':user_id,
        'avatar': avatar,
        'nickname': nickname,
        'formatted_last_update_time': formatted_date
    }
# def get_customized_article_data(json_str_data, for_web=False):
#     # json_str_data = get_article_page(note_id)
#
#     data = json.loads(json_str_data)['data']
#     note_data = data['items'][0]
#     customized_data = parse_note_data(note_data,for_web)
#     # 加入一个作者的信息
#     #先获取用户id
#     user_id = customized_data['user_id']
#     # print(user_id)
#     cnt = 0
#     customized_data['gender'] = 2
#     customized_data['ip_location'] = "未知"
#     # 请求两次，防止因为网络问题导致前面的用户信息正则匹配失败
#     while cnt < 2:
#         user_info = get_user_info(user_id)
#         if not user_info:
#             cnt += 1
#             continue
#         if 'gender' in user_info.keys():
#             customized_data['gender'] = user_info['gender']
#         if 'ipLocation' in user_info.keys():
#             customized_data['ip_location'] = user_info['ipLocation']
#
#     return customized_data
